package com.example.studentadminsystem

import android.app.AlertDialog
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.google.gson.Gson
import edu.njfu.networkdemo.model.ResultVO
import edu.njfu.networkdemo.service.UserService
import edu.njfu.networkdemo.utils.FileUtil
import edu.njfu.networkdemo.utils.ServerCreator
import kotlinx.android.synthetic.main.activity_main.*
import retrofit2.Call
import retrofit2.Response
import java.net.HttpURLConnection
import java.net.URL
import kotlin.concurrent.thread

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        login.setOnClickListener() {
            //获取用户名与密码
            val data = "userName=${et_username.text.toString()}&password=${et_password.text.toString()}"
            Toast.makeText(this,data, Toast.LENGTH_LONG).show()

            var intentOK=Intent(this,SecondActivity::class.java)

            val userService= ServerCreator.create(UserService::class.java)
            userService.checkLogin(et_username.text.toString(),et_password.text.toString()).enqueue(object :retrofit2.Callback<ResultVO>{
                override fun onFailure(call: retrofit2.Call<ResultVO>, t: Throwable) {
                      TODO("Not yet implemented")
                }

                override fun onResponse(
                    call: retrofit2.Call<ResultVO>,
                    response: retrofit2.Response<ResultVO>
                ) {
                    var resultVO=response.body()
                    Log.d("dsfdf",resultVO?.result)
                    if(resultVO?.result.toString()=="ok"){
                        startActivity(intentOK)
                    }else{
                        AlertDialog.Builder(this@MainActivity).apply {
                            setTitle("登录失败")
                            setMessage(resultVO?.result.toString())
                            setCancelable(false)
                            setPositiveButton("ok"){
                                    dialog, which ->
                            }
                            show()
                        }

                    }
                }
            })

            //sendRequestWithHttpURLConnection(data)
        }

        register.setOnClickListener(){
            val userService= ServerCreator.create(UserService::class.java)
            userService.register(et_username.text.toString(),et_password.text.toString()).enqueue(object :retrofit2.Callback<ResultVO> {
                override fun onFailure(call: retrofit2.Call<ResultVO>, t: Throwable) {
                    TODO("Not yet implemented")
                }

                override fun onResponse(
                    call: retrofit2.Call<ResultVO>,
                    response: retrofit2.Response<ResultVO>
                ){
                    var resultVO=response.body()
                    Log.d("dsfdf",resultVO?.result)
                    AlertDialog.Builder(this@MainActivity).apply {
                        setTitle("注册")
                        if(resultVO?.result.toString()=="ok") {
                            setMessage("注册成功")
                        }else{
                            setMessage("注册失败")
                        }
                        setCancelable(false)
                        setPositiveButton("ok"){
                                dialog, which ->
                        }
                        show()
                    }
                }
            })
        }
    }

}